<?php

namespace App\Controller;

use App\Interfaces\Controller\UserControllerInterface;
use App\Library\System\Model\AppModel;
use App\Library\System\Model\ProfileModel;
use App\Library\System\Model\UserModel;
use App\Library\System\Sub\Auth;

/**
 * User Controller Class
 *
 * @category   App
 * @package    App\Controller
 * @author     Loi Nguyen <loint@peslook.com>
 * @copyright  Peslook
 * @version    1.0
 * @since      Class available since Release 1.0
 */
class UserController extends BaseController implements UserControllerInterface
{

    /**
     * User model
     *
     * @var UserModel
     */
    public $user;

    /**
     * User ID
     *
     * @var int
     */
    public $uid;

    /**
     * Authentication
     *
     * @var Auth
     */
    public $auth;

    /**
     * App Model instance
     *
     * @var AppModel
     */
    public $app;

    /**
     * Index action
     *
     * @return array|void|\Zend\View\Model\ViewModel
     */
    public function indexAction()
    {
        $this->auth = new Auth();
        $this->app = AppModel::getInstance();

        $uid = $this->route('id');
        $this->user = new UserModel($uid);

        if (!$this->user->isValid()) {
            return $this->error(404);
        }

        // If you 're loginned and you 're in your page
        $allow_edit = ($this->auth->login) && ($this->auth->currentUser() == $this->user->id);

        if ($allow_edit) {
            // Profile for edit and build
            //$css = array('app', 'index', 'profile', 'user', 'jcrop');

            $less = array(
                'index_header',
                'user_social_widget',
                'user_slogan',
                'user_edit_content',
                'user_information',
                'user_jcrop',
                'user_profile',
                'user_widget',
                'user_upload',
                'app_languages',
                'app_footer'
            );

            $coffee = array(
                'index',
                'func',
                'user',
                'language',
                'profile',
                'lib/image',
                'lib/dragdrop',
                'lib/widget'
            );

            $layout = 'user';
        } else {

            // Profile is public to internet
            $less = array(
                'app',
                'index',
                'profile'
            );

            $coffee = array(
                'language'
            );

            $layout = 'profile';
        }

        $this->setLayout($layout, $less, $coffee);
        $page = $this->createPage('index');
        $this->func($page);

        $allow_edit ? $this->user($page) : $this->profile($page);

        $this->support($page);

        return $page;
    }

    /**
     * User page if login
     *
     * @route /{alias}
     *
     * @param $page
     */
    public function user($page)
    {
        $profile = new ProfileModel();
        $widgets = $profile->getWidgets();

        // Global layout
        $this->out->title = $this->user->getTitle();
        $this->out->description = $this->user->getDescription();
        $this->angular('user');

        $this->js('widgets', $this->json($widgets->toArray(), false));
        $this->js('widget_total', $widgets->count(), false);

        // Profile parameters
        $view = $this->getContentView($page);
        $this->func($view);

        $socials = $this->user->getSocial(array(
            'facebook',
            'skype',
            'linkedin',
            'github'
        ));

        $this->view($view, array(
            'user'    => $this->user,
            'socials' => $socials,
            'widgets' => $widgets
        ));
    }

    /**
     * Profile page if not login
     *
     * @route /{alias}
     *
     * @param $page
     */
    public function profile($page)
    {
        // Global layout
        $this->out->title = $this->user->getTitle();
        $this->out->description = $this->user->getDescription();

        // Profile parameters
        $page_view = $this->getContentView($page);
        $page_view->user = $this->user;
    }

}
